package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.sondon.mayi.events.TabHostChangeEvent;
import com.sondon.mayi.fragment.MyFragmentPage_;
import com.sondon.mayi.fragment.OrderFragmentPage_;
import com.sondon.mayi.fragment.ShopFragmentPage_;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.MyPrefs_;
import com.sondon.mayi.util.PreferencesUtil;

import de.greenrobot.event.EventBus;

/**
 * 
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : MainTabActivity
 * @Company 广州讯动网络科技有限公司
 * @Author : 蔡文锋
 * @DateTime：2015年5月30日 下午12:57:36
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : {}
 *
 */

@EActivity(R.layout.main_tab_layout)
public class MainTabActivity extends FragmentActivity {
	// 定义FragmentTabHost对象
	public FragmentTabHost mTabHost;
	@ViewById
	ImageView home_imageview;
	@ViewById
	ImageView order_imageview;
	@ViewById
	ImageView my_imageview;
	@ViewById
	ImageView red_hot;
	
	@ViewById
	TextView home_textview;
	@ViewById
	TextView order_textview;
	@ViewById
	TextView my_textview;
	
	
	@ViewById
	LinearLayout home_linearlayout;
	@ViewById
	LinearLayout order_linearlayout;
	@ViewById
	LinearLayout my_linearlayout;
	
	@Pref
	MyPrefs_ myprefs;
	
	// 定义一个布局
	private LayoutInflater layoutInflater;

	// 定义数组来存放Fragment界面
	private Class<?> fragmentArray[] = { ShopFragmentPage_.class,
			OrderFragmentPage_.class, MyFragmentPage_.class };
	// 定义数组来存放按钮图片
	private int mImageViewArray[] = { R.drawable.tab_home_btn,
			R.drawable.tab_order_btn, R.drawable.tab_my_btn, };

	// Tab选项卡的文字
	private String mTextviewArray[] = { "首页", "订单", "我的" };

	
	/**
	 * 初始化
	 */
	@AfterViews
	public void initView() {
		LogUtil.e("@AfterViews", "@AfterViews....");
		
		// 实例化布局对象
		layoutInflater = LayoutInflater.from(this);

		// 实例化TabHost对象，得到TabHost
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		// 得到fragment的个数
		int count = fragmentArray.length;

		
		for (int i = 0; i < count; i++) {
			// 为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
			// 设置Tab按钮的背景
//			mTabHost.getTabWidget().getChildAt(i).setBackgroundColor();
		}
		
		//还原默认状态
		initTabhost();
		
		//初始选中第一个tab
		home_imageview.setSelected(true);
		home_textview.setTextColor(getResources().getColor(R.color.tabhost_txt_pressed));
		
		//注册EventBus
		EventBus.getDefault().register(this);
		
	}
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		//注销EventBus
		EventBus.getDefault().unregister(this);
	}
	
	//Activity的启动模式(launchMode),通过这个方法接受Intent
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		LogUtil.e("MainTabActivity", " onNewIntent...");
		handleIntent(intent);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		LogUtil.e("MainTabActivity", " onStart...");
		handleIntent(getIntent());
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		int position =PreferencesUtil.getIntValue("position");
		setTabHost(position);
	}
	
	
	//点击事件
	@Click({ R.id.home_linearlayout, R.id.order_linearlayout, R.id.my_linearlayout })
	public void onClick(View v) {
		//还原默认状态
//		initTabhost();
		switch (v.getId()) {
		case R.id.home_linearlayout:
			setTabHost(0);
			break;
		case R.id.order_linearlayout:
			String news_code=myprefs.news_code().get();
			if("1".equals(news_code)){
				myprefs.news_code().put("");
			}
			
			setTabHost(1);
			break;
		case R.id.my_linearlayout:
			setTabHost(2);
			break;

		default:
			break;
		}
	}
	
	/**
	 * @Author 蔡文锋
	 * @Data_Time 2015年7月16日 下午10:37:12
	 * @Description { 处理Intent }
	 * @param intent
	 */
	private void handleIntent(Intent intent){
		if(intent!=null && intent.getExtras()!=null){
			String news_code=intent.getExtras().getString("news_code","");
//			LogUtil.e("MainTabActivity", "news_code  :"+news_code);
			myprefs.news_code().put(news_code);
		}
	}
	
	/**
	 * @Author 蔡文锋
	 * @Data_Time 2015年6月17日 上午12:38:50
	 * @Description { 还原默认状态 }
	 */
	public void initTabhost(){
		
		String news_code=myprefs.news_code().get();
//		String news_code=MainTabActivity.this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).getString("news_code", "");
//		LogUtil.e("MainTabActivity", "news_code  :"+news_code);
		if("1".equals(news_code)){
			red_hot.setVisibility(View.VISIBLE);
		}else{
			red_hot.setVisibility(View.GONE);
		}
		
		home_imageview.setSelected(false);
		order_imageview.setSelected(false);
		my_imageview.setSelected(false);
		
		home_textview.setTextColor(getResources().getColor(R.color.tabhost_txt_nor));
		order_textview.setTextColor(getResources().getColor(R.color.tabhost_txt_nor));
		my_textview.setTextColor(getResources().getColor(R.color.tabhost_txt_nor));
	}

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(int index) {
		View view = layoutInflater.inflate(R.layout.tab_item_view, null);
//
//		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
//		imageView.setImageResource(mImageViewArray[index]);
//
//		TextView textView = (TextView) view.findViewById(R.id.textview);
//		textView.setText(mTextviewArray[index]);

		return view;
	}
	
	//TabHost切换Item事件
	public void onEventMainThread(TabHostChangeEvent event) {
		//上面的onClick方法已经有相应的切换处理,所以这里只要模拟按钮点击
		LogUtil.e("MainTabActivity", "MainTabActivity  eventBus  position:"+event.getPosition());
		setTabHost(event.getPosition());
	}
	
	
	//切换TabHost
	public void setTabHost(int position) {
		// 还原默认状态
		initTabhost();
		switch (position) {
		case 0:// 首页
			mTabHost.setCurrentTab(0);
			home_imageview.setSelected(true);
			home_textview.setTextColor(getResources().getColor(
					R.color.tabhost_txt_pressed));
			break;
		case 1:// 订单
			
			mTabHost.setCurrentTab(1);
			order_imageview.setSelected(true);
			order_textview.setTextColor(getResources().getColor(
					R.color.tabhost_txt_pressed));
			break;
		case 2:// 我的
			mTabHost.setCurrentTab(2);
			my_imageview.setSelected(true);
			my_textview.setTextColor(getResources().getColor(
					R.color.tabhost_txt_pressed));
			break;
		default:
			break;
		}
	}
}